package com.example.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.Result;
import com.example.entry.RiderOrder;
import com.example.service.IRiderOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/*
 * 骑手订单后台管理控制器
 * */
@RequestMapping("/admin/rider-order")
@RestController
public class RiderOrderController {

    @Autowired
    private IRiderOrderService riderOrderService;

    /*
     * 根据id查询信息
     * */
    @GetMapping("/rider-order-id={id}")
    public Result getRiderOrderOne(@PathVariable Integer id){
        RiderOrder byId = riderOrderService.getById(id);
        return Result.success(byId);
    }

    /*
     * 根据id进行删除
     * */
    @GetMapping("/delete/id={id}")
    public Result deleteRiderOrderInfo(@PathVariable Integer id) {
        riderOrderService.removeById(id);
        return Result.success("删除成功");
    }

    /*
     * 修改或保存信息
     * */
    @PostMapping("/update")
    public Result updateOrSavRiderOrder(@RequestBody RiderOrder riderOrder){
        riderOrderService.saveOrUpdate(riderOrder);
        return Result.success("更新成功");
    }

    /*
     * 分页查询
     * */
    @GetMapping("/page")
    public Result getPageRiderOrder(@RequestParam Integer pageNum,
                                 @RequestParam Integer pageSize,
                                 @RequestParam(defaultValue = "1") Integer status,
                                 @RequestParam(defaultValue = "") String username,
                                 @RequestParam(defaultValue = "") String phone,
                                 @RequestParam(defaultValue = "") String nickname){
        QueryWrapper<RiderOrder> queryWrapper = new QueryWrapper<>();
        // 根据id进行降序排序
        queryWrapper.orderByDesc("id");
        // 查询管理员和普通用户
        if(!"".equals(status)){
            queryWrapper.like("status", status);
        }
        // 根据条件进行模糊查询
        if(!"".equals(username)){
            queryWrapper.like("username", username);
        }
        if(!"".equals(phone)){
            queryWrapper.like("phone", phone);
        }
        if(!"".equals(nickname)){
            queryWrapper.like("nickname", nickname);
        }
        // 分页构造器
        Page<RiderOrder> page = riderOrderService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return Result.success(page);
    }
}
